package org.nt4b.schemadoc.webgui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;


/**
 * Model class used in a login page for connection selection.
 * Either an predefined connection can be selected,
 * or new connection information inputed.
 * 
 */
public class ConnectionConfiguration implements Serializable {
    
    private static final long serialVersionUID = -3253308675228934229L;
    private static final String EMPTY_LIST_ITEM = "#empty#";
    
    private List<SelectItem> connections;
    private String selectedConnectionName;
    private List<ConnectionProperties> connectionProperties;
    
    private String url;
    private String username;
    private String password;
    private String schemaName;
    
    public ConnectionProperties getConnectionProperties() {
        if (EMPTY_LIST_ITEM.equals(selectedConnectionName)) {
            final ConnectionProperties result = new ConnectionProperties();
            result.setName("User input connection");
            result.setUrl(url);
            result.setUsername(username);
            result.setPassword(password);
            return result;
        }
        
        for (final ConnectionProperties properties : connectionProperties) {
            if (properties.getName().equals(selectedConnectionName)) {
                return properties;
            }
        }
        
        return null;
    }
    
    public List<SelectItem> getConnections() {
        return connections;
    }
    
    public void setConnections(final List<SelectItem> connections) {
        this.connections = connections;
    }
    
    public String getSelectedConnectionName() {
        return selectedConnectionName;
    }
    
    public void setSelectedConnectionName(final String selectedConnectionName) {
        this.selectedConnectionName = selectedConnectionName;
    }
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(final String url) {
        this.url = url;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(final String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(final String password) {
        this.password = password;
    }
        
    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    /**
     * Initialize internal list of connection's.
     * Reset selected connection configuration.
     * 
     * @param connectionPropertiesList
     */
    public void init(final List<ConnectionProperties> connectionProperties) {
        final List<SelectItem> result = new ArrayList<SelectItem>();
        final SelectItem emptyItem = new SelectItem(EMPTY_LIST_ITEM, "");
        result.add(emptyItem);
        
        for (final ConnectionProperties properties : connectionProperties) {
            final SelectItem selectItem = new SelectItem(properties.getName(), properties.getName());
            result.add(selectItem);
        }
        
        connections = result;
        selectedConnectionName = EMPTY_LIST_ITEM;
        this.connectionProperties = connectionProperties;
    }
    
}
